'use strict';
import React, { Component,PropTypes } from 'react'

import {
  Alert
}from 'react-native';

export default class DCStore {

  constructor() {

    this.bindActions(this.alt.getActions('DCAction'));

    this.state = {
      disabled: false,

      username: undefined,
      name: undefined,
      numLunch: [],
      numSupper: [],
      lObjId: undefined,
      sObjId: undefined,
      dcUsersLNum: [],
      dcUsersSNum: [],

      users: [],
      usersList: [],
      usersData: [],
      rowID: undefined,
      dcNum: [],
      lastDcNum: [],

      isShowHud:false,
      hudText: undefined,
    };
  }

  onSetNavBarWithRoute(route){
    if(Platform.OS=='ios'){
            if(route.statusBarStyle!== undefined && route.statusBarStyle!== Common.statusBarStyle){
              StatusBarIOS.setStyle(route.statusBarStyle,true);
              Common.statusBarStyle = route.statusBarStyle;
            }else if(Common.statusBarStyle !== this.state.apiSet.statusBarStyle){
              StatusBarIOS.setStyle(this.state.apiSet.statusBarStyle,true);
              Common.statusBarStyle = this.state.apiSet.statusBarStyle;
              }
            }

        if (route.isNavBarHidden !== undefined && this.state.isNavBarHidden !== route.isNavBarHidden) {
      this.setState({isNavBarHidden: route.isNavBarHidden});
    }else if(this.state.isNavBarHidden == true){
      //默认不隐藏.
      this.setState({isNavBarHidden:false});
    }else{
      return false;
    }
  }
  onIsClick(state) {
    // alert(state)
    this.setState({disabled: state})
  }
  onUsers(users) {
    this.state.users = [];
    this.state.usersList = [];
    users.map((data,index)=>{
       this.state.users.push(data.username)
       this.state.usersList.push(data.name)
    })
  }

  onUsersData(params) {
    var date;
    var year;
    var mon;
    
    var obj;

    this.state.usersData = [];
    params.map((data, index)=>{
        date = data.createdAt;
        year = date.substring(0,4)
        mon = date.substring(5,7)

        if (year == new Date().getFullYear() 
            && mon == new Date().getMonth()+1) {

            for(var i = 0; i < this.state.users.length; i++){

              if (data.username == this.state.users[i]) {
                
                  var numLunch =0;
                  var numSupper = 0 ;
                  if (this.state.usersData[i]) {
                      numLunch = this.state.usersData[i].lunch
                      numSupper = this.state.usersData[i].supper
                  };


                  if (data.type.indexOf('0') > -1) {
                      numLunch +=1  

                  }else if (data.type.indexOf('1') > -1) {
                      numSupper +=1
                  }

                  obj = {users: data.username,
                         name: data.name,
                         lunch: numLunch,
                         supper: numSupper
                         }
                this.state.usersData[i] = obj

              }
            }
            

        }    

    })
 
    
  }

  onSetUsername(result) {
    this.state.username = result.username;
    this.state.name = result.name;
  }

  onObjID(result){

    var date;
    var year;
    var mon;
    var day;
    
    var nowMon = new Date().getMonth()+1;
    if (nowMon < 10) {
        nowMon = '0'+nowMon
    };
    this.state.numLunch.splice(0,this.state.numLunch.length)
    this.state.numSupper.splice(0,this.state.numSupper.length)

        result.results.map((data, index)=>{
          date = data.time;
          if (date) {
            year = date.substring(0,4)
            mon = date.substring(5,7)
            day = date.substring(8,10)

          }

            if (data.username == this.state.username) {

                if (year == new Date().getFullYear() 
                    && mon == nowMon 
                    && data.type != undefined) {
                  if (data.type.indexOf('0') > -1) {
                     
                      this.state.numLunch.push(day)
                      this.state.lObjId = data.objectId

                  }else if (data.type.indexOf('1') > -1) {

                      this.state.numSupper.push(day)
                      this.state.sObjId = data.objectId

                  };
                    
                };
            };


        })
    
  }
  onTodayDC(result) {
    var date;
    var year;
    var mon;
    var day;


    this.state.dcUsersLNum.splice(0,this.state.dcUsersLNum.length)
    this.state.dcUsersSNum.splice(0,this.state.dcUsersSNum.length)
     //本日累计点餐人数
      result.results.map((data, index)=>{

        date = data.time;
          if (date) {
            year = date.substring(0,4)
            mon = date.substring(5,7)
            day = date.substring(8,10)

          }

      if (year == new Date().getFullYear() 
          && mon == new Date().getMonth()+1
          && day == new Date().getDate()) {
              
          if (data.type.indexOf('0') > -1) {

              this.state.dcUsersLNum.push(data.username)                      

          }else if (data.type.indexOf('1') > -1) {
                  
              this.state.dcUsersSNum.push(data.username)
                     
          };
      };
    })
  }
  //储存数据统计的rowID
  onSetRowID(rowID) {
    this.state.rowID = rowID
  }

  onSetDCNum(result) {
    var obj;
    var date;

    this.state.dcNum.splice(0,this.state.dcNum.length)

    result.results.map((data, index)=>{
      if (data.time) {
          date = data.time.substring(0,10);
      };

      if (data.name == this.state.rowID) {
          if (data.type.indexOf('0') > -1) {

              obj = ({type: '☀',
                            date: date,
                            })                     
          }else if (data.type.indexOf('1') > -1) {
                  
              obj = ({type: '🌙',
                            date: date,
                            })
          };
          
          this.state.dcNum.splice(0, 0, obj)
      };
    })


  }

  onSetLastDCNum(result) {
    var obj;
    var date;

    this.state.lastDcNum.splice(0,this.state.lastDcNum.length)

    result.results.map((data, index)=>{
      if (data.time) {
          date = data.time.substring(0,10);
      };

      if (data.name == this.state.rowID) {
          if (data.type.indexOf('0') > -1) {

              obj = ({type: '☀',
                            date: date,
                            })                     
          }else if (data.type.indexOf('1') > -1) {
                  
              obj = ({type: '🌙',
                            date: date,
                            })
          };
          
          this.state.lastDcNum.splice(0, 0, obj)
      };
    })

    
  }

  onHideText(inPut) {
    this.setState({isShowHud: false, hudText: undefined})
  }

  onShowLoading(){
    this.state.isShowHud = true;
   
  }

  onHideLoading(){
    this.state.isShowHud = false;
    
  }
  onShowText(inPut) {
    this.setState({isShowHud: true, hudText: inPut.text})
    Alert.alert(
            '提示',
            inPut.text
          )
  }
  
}



